//
//  leftAlertView.m
//  LzhAlertView
//
//  Created by 刘中华 on 2019/12/11.
//  Copyright © 2019 LZH. All rights reserved.
//  左侧   抽屉

#import "leftAlertView.h"
#import "UIColor+LZHExtension.h"
#import "UIButton+ImageTitleSpacing.h"
#import "ToolTipView.h"
#import "LoginViewController.h"

/* 屏幕尺寸 */
#define Screen_W   [UIScreen mainScreen].bounds.size.width
#define Screen_H   [UIScreen mainScreen].bounds.size.height
#define kMaxX(X) CGRectGetMaxX(X)
#define kMaxY(Y) CGRectGetMaxY(Y)

@interface leftAlertView ()

@property(nonatomic,strong)NSArray * titleArr ;
@property(nonatomic,strong)UIView * bgView ;
@property(nonatomic,strong)UIButton * infoBtn ;
@property(nonatomic,strong)UIImageView * headerImg ;
@property(nonatomic,strong)UILabel * titleLabel ;
@property(nonatomic,strong)UILabel * subTitleLabel ;

@end

@implementation leftAlertView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
        [self showAlertView];
    }
    return self;
}

#pragma mark -- 事件
//显示
- (void)showAlertView{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"000000" alpha:0.4] ;
        self.bgView.frame = CGRectMake(0, 0, 300, Screen_H) ;
    }];
}

//隐藏
- (void)removeAlertView{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"000000" alpha:0.0] ;
        self.bgView.frame = CGRectMake(-300, 0, 300, Screen_H) ;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)infoAction{
    [ToolTipView showMessage:@"点击头像" offset:-15];
    [self removeAlertView];
}


#pragma mark -- UI
- (void)createSubviews{
    self.backgroundColor = [UIColor colorWithString:@"000000" alpha:0.0] ;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeAlertView)]];
    
    [self addSubview:self.bgView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    view.backgroundColor = [UIColor colorWithRed:125/255.0 green:120/255.0 blue:164/255.0 alpha:1.0];
    [self.bgView addSubview:view];
    
    [view addSubview:self.headerImg];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.subTitleLabel];
    
    UIButton *tuichuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tuichuButton.frame = CGRectMake(10, Screen_H-70, 300-20, 40);
    [tuichuButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [tuichuButton setBackgroundColor:[UIColor colorWithRed:241/255.0 green:247/255.0 blue:253/255.0 alpha:1.0]];
    [tuichuButton setTitleColor:[UIColor systemBlueColor] forState:0];
    tuichuButton.layer.cornerRadius = 10;
    tuichuButton.layer.masksToBounds = YES;
    [tuichuButton addTarget: self action:@selector(tuiChu:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:tuichuButton];
}

- (void)tuiChu:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否退出登录" message: nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // 删除NSUserDefaults所有记录
        NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
        NSUserDefaults * defautls = [NSUserDefaults standardUserDefaults];
        [defautls removePersistentDomainForName:appDomain];
        
        LoginViewController *vc = [[LoginViewController alloc] init];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = vc;
        [window makeKeyAndVisible];
    }]];
    [[self topViewController] presentViewController:alert animated:YES completion:nil];
    
}

/// 获取最顶层控制器
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(-300, 0, 300, Screen_H)];
        _bgView.backgroundColor = [UIColor whiteColor] ;
    }
    return _bgView ;
}

- (UIButton *)infoBtn{
    if (!_infoBtn) {
        _infoBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 300, 64)];
        [_infoBtn setBackgroundColor:[UIColor whiteColor]] ;
        [_infoBtn addTarget:self action:@selector(infoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _infoBtn ;
}

- (UIImageView *)headerImg{
    if (!_headerImg) {
        _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(300/2-64/2, 200/2-64/2, 64, 64)];
        _headerImg.image = [UIImage imageNamed:@"AppIcon"] ;
        _headerImg.layer.cornerRadius = 15 ;
        _headerImg.clipsToBounds = YES ;
    }
    return _headerImg ;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 215, 150, 30)];
        _titleLabel.text = @"欢迎!" ;
        _titleLabel.textAlignment = 0;
        _titleLabel.textColor = [UIColor colorWithString:@"333333"] ;
        _titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightMedium];
    }
    return _titleLabel ;
}

-(UILabel *)subTitleLabel{
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 245, 280, 100)];
        _subTitleLabel.text = [NSString stringWithFormat:@"超级管理员\n本次登录时间是:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"teacherLoginTime"]];
        _subTitleLabel.textAlignment = 0;
        _subTitleLabel.numberOfLines = 0;
        _subTitleLabel.textColor = [UIColor colorWithString:@"333333"] ;
        _subTitleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _subTitleLabel ;
}

@end
